﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;

using LIM.model;
using LIM.controller;

using LIM.forms.uc_CRM_PhieuNhanMau_ChiTiet;
using Telerik.WinControls.Data;
using LIM.report;

namespace LIM.forms
{
    public partial class radForm_CRM_PhieuNhanMau_ChiTiet : Telerik.WinControls.UI.RadForm
    {
        public radForm_CRM_PhieuNhanMau_ChiTiet()
        {
            InitializeComponent();
            //this.radLabel_NoteForDelete.AutoSize = false;

            if (tbl_CRM_PhieuNhanMau == null)
            {
                tbl_CRM_PhieuNhanMau = new tbl_CRM_PhieuNhanMau();
                tbl_CRM_PhieuNhanMau.NguoiTao = InitUserInfos.UserName;

                this.radDateTimePicker_NgayGioNhanMau.Value = this.radDateTimePicker_NgayGioHenTraKetQua.Value = CRM_PhieuNhanMauController.GetCurrentDateTimeFromDatabaseServer();
                this.radLabel_NgayTao.Text = "Ngày tạo: ";
                this.radLabel_NhanVienTao.Text = "Nhân viên tạo: ";
            }
            
            UserControls_Load();
        }
        public radForm_CRM_PhieuNhanMau_ChiTiet(int RowID)
        {
            InitializeComponent();
            //this.radLabel_NoteForDelete.AutoSize = false;

            this.tbl_CRM_PhieuNhanMau = CRM_PhieuNhanMauController.loadPhieuNhanMauByID(RowID);
            
            UserControls_Load();
        }

        public tbl_CRM_PhieuNhanMau tbl_CRM_PhieuNhanMau { set; get; }
        public KhachHangInfo KhachHangInfo { set; get; }

        public delegate void LuuLai_Click();
        public event LuuLai_Click OnLuuLai_Click;

        public delegate void KhachHang_Changed();
        public event KhachHang_Changed OnKhachHang_Changed;

        public delegate void Data_Changed();
        public event Data_Changed OnData_Changed;

        public CRM_PhieuNhanMauController CRM_PhieuNhanMauController = new CRM_PhieuNhanMauController();
        DanhMucKhachHangController DanhMucKhachHangController = new DanhMucKhachHangController();

        private void UserControls_Load()
        {
            uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem = new uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem();
            uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem.Dock = DockStyle.Fill;
            this.radPageViewPage_ChiTieuKiemNghiem.Controls.Add(uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem);

            uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan = new uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan();
            uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan.Dock = DockStyle.Fill;
            this.radPageViewPage_ThongTinBenNhan.Controls.Add(uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan);
            uc_CRM_PhieuNhanMau_ChiTiet_ThongTinBenNhan.DelegateEvent_Load();

            uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau = new uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau();
            uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau.Dock = DockStyle.Fill;
            this.radPageViewPage_LuuTachMau.Controls.Add(uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau);

            //uc_CRM_PhieuNhanMau_ChiTiet_FileDinhKem uc_CRM_PhieuNhanMau_ChiTiet_FileDinhKem = new uc_CRM_PhieuNhanMau_ChiTiet_FileDinhKem();
            //uc_CRM_PhieuNhanMau_ChiTiet_FileDinhKem.Dock = DockStyle.Fill;
            //this.radPageViewPage_FileDinhKem.Controls.Add(uc_CRM_PhieuNhanMau_ChiTiet_FileDinhKem);
            uc_FileDinhKem uc_FileDinhKem = new uc_FileDinhKem();
            uc_FileDinhKem.Dock = DockStyle.Fill;
            uc_FileDinhKem.Name = "uc_FileDinhKem_PhieuNhanMau";
            uc_FileDinhKem.Path = "/LIMS/PhieuNhanMau";
            this.radPageViewPage_FileDinhKem.Controls.Add(uc_FileDinhKem);

            uc_CRM_PhieuNhanMau_ChiTiet_NhatKyLienLac uc_CRM_PhieuNhanMau_ChiTiet_NhatKyLienLac = new uc_CRM_PhieuNhanMau_ChiTiet_NhatKyLienLac();
            uc_CRM_PhieuNhanMau_ChiTiet_NhatKyLienLac.Dock = DockStyle.Fill;
            this.radPageViewPage_NhatKyLienLac.Controls.Add(uc_CRM_PhieuNhanMau_ChiTiet_NhatKyLienLac);
        }
                
        private void radForm_CRM_PhieuNhanMau_ChiTiet_Load(object sender, EventArgs e)
        {
            this.radLabel_SoTienConPhaiThu.AutoSize = false;
            this.radLabel_SoTienConPhaiThu.TextAlignment = ContentAlignment.MiddleRight;

            maximumSizeOfPanel1 = splitPanel1.Height;
                        
            SetNewOrUpdateState();

            LoadKhachHangComboBox();
            LoadTinhTrangComboBox();

            DataLoad();
        }
        private void DataLoad()
        {
            if (tbl_CRM_PhieuNhanMau.RowID > 0)
            {
                // Select Item
                UtilityClass.RadMultiColumnComboBoxSelectValue(this.radDropDownList_KhachHang, tbl_CRM_PhieuNhanMau.RowIDKhachHang);
                
                this.radDateTimePicker_NgayGioNhanMau.Value = tbl_CRM_PhieuNhanMau.NgayGioNhanMau.Value;
                this.radDateTimePicker_NgayGioHenTraKetQua.Value = tbl_CRM_PhieuNhanMau.NgayGioTraKetQua.Value;
                this.radTextBox_MaBaoGiaMaHopDong.Text = tbl_CRM_PhieuNhanMau.ThuocHopDongSo;
                this.radDropDownList_TinhTrangPhieuNhanMau.SelectedValue = tbl_CRM_PhieuNhanMau.TinhTrang;
                this.radCheckBox_ThucHienGap.Checked = tbl_CRM_PhieuNhanMau.PhaiThucHienGap.GetValueOrDefault(false);
                this.radCheckBox_TraKetQua_Email.Checked = tbl_CRM_PhieuNhanMau.TraKetQua_Email.GetValueOrDefault(false);
                this.radCheckBox_TraKetQua_Fax.Checked = tbl_CRM_PhieuNhanMau.TraKetQua_Fax.GetValueOrDefault(false);
                this.radCheckBox_TraKetQua_DT.Checked = tbl_CRM_PhieuNhanMau.TraKetQua_DT.GetValueOrDefault(false);
                this.radCheckBox_TraKetQua_Thu.Checked = tbl_CRM_PhieuNhanMau.TraKetQua_Thu.GetValueOrDefault(false);
                this.radCheckBox_TraKetQua_TrucTiep.Checked = tbl_CRM_PhieuNhanMau.TraKetQua_TrucTiep.GetValueOrDefault(false);

                this.radTextBox_NhaThauPhu.Text = tbl_CRM_PhieuNhanMau.NhaThauPhu;
                this.radTextBox_YeuCauKhac.Text = tbl_CRM_PhieuNhanMau.YeuCauKhac;
                this.radTextBox_GhiChuKeToan.Text = tbl_CRM_PhieuNhanMau.GhiChuDanhChoKeToan;

                this.radLabel_NgayTao.Text = "Ngày tạo: " + tbl_CRM_PhieuNhanMau.NgayTao.Value.ToString("dd/MM/yyyy HH:mm");
                this.radLabel_NhanVienTao.Text = "Nhân viên tạo: " + tbl_CRM_PhieuNhanMau.NguoiTao;

                if (OnData_Changed != null)
                    OnData_Changed();
            }
        }

        int minimumSizeOfPanel1 = 30;
        int maximumSizeOfPanel1 = 204;
        private void ZoomInOut_Click(object sender, EventArgs e)
        {
            if (splitPanel1.Height == minimumSizeOfPanel1)
            {
                this.splitPanel1.SizeInfo.AbsoluteSize = new Size(this.splitPanel1.Width, maximumSizeOfPanel1);
                this.radButton_ZoomIn.BringToFront();
            }
            else
            {
                this.splitPanel1.SizeInfo.AbsoluteSize = new Size(this.splitPanel1.Width, minimumSizeOfPanel1);
                this.radButton_ZoomOut.BringToFront();
            }            
        }

        private void SetNewOrUpdateState()
        {
            if (tbl_CRM_PhieuNhanMau.RowID > 0)
            {
                this.radPageViewPage_FileDinhKem.Enabled = true;
                this.radPageViewPage_NhatKyLienLac.Enabled = true;
                this.radPageViewPage_LuuTachMau.Enabled = true;
                this.radPageViewPage_NhatKyKiemNghiem.Enabled = true;
            }
            else
            {
                this.radPageViewPage_FileDinhKem.Enabled = false;
                this.radPageViewPage_NhatKyLienLac.Enabled = false;
                this.radPageViewPage_LuuTachMau.Enabled = false;
                this.radPageViewPage_NhatKyKiemNghiem.Enabled = false;
            }
        }

        #region Copy code from Thanh
        System.Windows.Forms.BindingSource _KhachHangBindingSource = new BindingSource();
        System.Windows.Forms.BindingSource _TinhTrangBindingSource = new BindingSource();

        bool IsKHLoaded = false;
        // Load Khach hang
        void LoadKhachHangComboBox()
        {
            // Lay danh sach khach hang.            
            _KhachHangBindingSource.DataSource = DanhMucKhachHangController.LoadKhanhHang_PhieuNhanMau();

            radDropDownList_KhachHang.DataSource = null;

            // Gan danh sach vao combobox.
            if (true)
            {
                IsKHLoaded = false;
                this.radDropDownList_KhachHang.DisplayMember = "TenGoi";
                this.radDropDownList_KhachHang.ValueMember = "RowID";
                this.radDropDownList_KhachHang.DataSource = _KhachHangBindingSource;
                // Set height layout.
                this.radDropDownList_KhachHang.MultiColumnComboBoxElement.DropDownSizingMode = Telerik.WinControls.UI.SizingMode.UpDownAndRightBottom;
                this.radDropDownList_KhachHang.MultiColumnComboBoxElement.DropDownMinSize = new Size(0, 200);
                this.radDropDownList_KhachHang.MultiColumnComboBoxElement.AutoSizeDropDownToBestFit = true;
                
                //Filter TenGoi
                FilterDescriptor oFilterDescriptorTenGoi = new FilterDescriptor("TenGoi", FilterOperator.Contains, "");
                oFilterDescriptorTenGoi.IsFilterEditor = true;
                this.radDropDownList_KhachHang.EditorControl.MasterTemplate.FilterDescriptors.Add(oFilterDescriptorTenGoi);

                FilterDescriptor oFilterDescriptorMaKhachHang = new FilterDescriptor("MaKhachHang", FilterOperator.Contains, null);
                oFilterDescriptorMaKhachHang.IsFilterEditor = true;
                this.radDropDownList_KhachHang.EditorControl.MasterTemplate.FilterDescriptors.Add(oFilterDescriptorMaKhachHang);

                FilterDescriptor oFilterDescriptorTenGoiNgan = new FilterDescriptor("TenGoiNgan", FilterOperator.Contains, null);
                oFilterDescriptorTenGoiNgan.IsFilterEditor = true;
                this.radDropDownList_KhachHang.EditorControl.MasterTemplate.FilterDescriptors.Add(oFilterDescriptorTenGoiNgan);
                //this.radDropDownList_KhachHang.MultiColumnComboBoxElement.EditorControl.Columns["TenGoi"].FilterDescriptor = oFilterDescriptorTenGoi;

                this.radDropDownList_KhachHang.EditorControl.MasterTemplate.FilterDescriptors.LogicalOperator = FilterLogicalOperator.Or;

                this.radDropDownList_KhachHang.MultiColumnComboBoxElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend;                
                //this.radDropDownList_KhachHang.AutoFilter = true;
                                
                this.radDropDownList_KhachHang.SelectedIndex = -1;
                IsKHLoaded = true;
            }
        }

        // Load Tinh Trang
        void LoadTinhTrangComboBox()
        {
            DanhMucHeThongController DanhMucHeThongController = new controller.DanhMucHeThongController();
            // Lay danh sach tinh trang.
            _TinhTrangBindingSource.DataSource = DanhMucHeThongController.LoadDataChild("TinhTrangPhieuNhanMau");
            radDropDownList_TinhTrangPhieuNhanMau.SelectedIndex = -1;

            radDropDownList_TinhTrangPhieuNhanMau.DataSource = null;
            
            if (true)
            {
                radDropDownList_TinhTrangPhieuNhanMau.DisplayMember = "TenGoi";
                radDropDownList_TinhTrangPhieuNhanMau.ValueMember = "MaDanhMuc";
                radDropDownList_TinhTrangPhieuNhanMau.DataSource = _TinhTrangBindingSource;
                radDropDownList_TinhTrangPhieuNhanMau.SelectedIndex = 0;
            }
        }

        #endregion

        private void radMenuItem_LuuLai_Click(object sender, EventArgs e)
        {               
            tbl_CRM_PhieuNhanMau.NgayGioNhanMau = this.radDateTimePicker_NgayGioNhanMau.Value;
            tbl_CRM_PhieuNhanMau.NgayGioTraKetQua = this.radDateTimePicker_NgayGioHenTraKetQua.Value;
            tbl_CRM_PhieuNhanMau.ThuocHopDongSo = this.radTextBox_MaBaoGiaMaHopDong.Text;
            tbl_CRM_PhieuNhanMau.TinhTrang = this.radDropDownList_TinhTrangPhieuNhanMau.SelectedValue.ToString();
            tbl_CRM_PhieuNhanMau.PhaiThucHienGap = this.radCheckBox_ThucHienGap.Checked;
            tbl_CRM_PhieuNhanMau.TraKetQua_Email = this.radCheckBox_TraKetQua_Email.Checked;
            tbl_CRM_PhieuNhanMau.TraKetQua_Fax = this.radCheckBox_TraKetQua_Fax.Checked;
            tbl_CRM_PhieuNhanMau.TraKetQua_DT = this.radCheckBox_TraKetQua_Fax.Checked;
            tbl_CRM_PhieuNhanMau.TraKetQua_Thu = this.radCheckBox_TraKetQua_DT.Checked;
            tbl_CRM_PhieuNhanMau.TraKetQua_TrucTiep = this.radCheckBox_TraKetQua_TrucTiep.Checked;

            tbl_CRM_PhieuNhanMau.NhaThauPhu = this.radTextBox_NhaThauPhu.Text;
            tbl_CRM_PhieuNhanMau.YeuCauKhac = this.radTextBox_YeuCauKhac.Text;
            tbl_CRM_PhieuNhanMau.GhiChuDanhChoKeToan = this.radTextBox_GhiChuKeToan.Text;

            if (tbl_CRM_PhieuNhanMau.RowID <= 0)
                tbl_CRM_PhieuNhanMau.NgayTao = CRM_PhieuNhanMauController.GetCurrentDateTimeFromDatabaseServer();
                        
            tbl_CRM_PhieuNhanMau.NgayCapNhat = CRM_PhieuNhanMauController.GetCurrentDateTimeFromDatabaseServer();
            tbl_CRM_PhieuNhanMau.NguoiCapNhat = InitUserInfos.UserName;

            if (OnLuuLai_Click != null)
                OnLuuLai_Click();

            string outMess = "";
            int outRowID = 0;
            if (CRM_PhieuNhanMauController.SaveObject(tbl_CRM_PhieuNhanMau, out outMess, out outRowID))
            {
                this.radTextBox_MaPhieuNhanMau.Text = tbl_CRM_PhieuNhanMau.MaPhieuNhanMau;
                this.radLabel_NgayTao.Text = "Ngày tạo: " + tbl_CRM_PhieuNhanMau.NgayTao.Value.ToString("dd/MM/yyyy HH:mm");
                this.radLabel_NhanVienTao.Text = "Nhân viên tạo: " + tbl_CRM_PhieuNhanMau.NguoiTao;
                RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Phiếu nhận mẫu", "Đã lưu thành công!");
                SetNewOrUpdateState();
            }
            else
                MessageBox.Show(outMess);

            // Refresh Mau được nhận -> Load barcode mau.
            CRM_PhieuNhanMauController.Refresh_MauDuocNhan(tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan);
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {

        }

        private void radMenuItem_XoaBo_Click(object sender, EventArgs e)
        {

        }

        private void radMenuItem_TaoMoiCopy_Click(object sender, EventArgs e)
        {

        }
        private void radMenuItem_TaoPhieuThu_Click(object sender, EventArgs e)
        {
            radForm_CRM_PhieuThu_ChiTiet form = new radForm_CRM_PhieuThu_ChiTiet();
            form.MdiParent = this.MdiParent;
            form.Show();
        }
        private void radDropDownList_KhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!IsKHLoaded)
                return;
            if (this.radDropDownList_KhachHang.SelectedItem is GridViewDataRowInfo)
            {
                KhachHangInfo = (KhachHangInfo)((GridViewDataRowInfo)this.radDropDownList_KhachHang.SelectedItem).DataBoundItem;
                if (KhachHangInfo != null)
                {
                    tbl_CRM_PhieuNhanMau.RowIDKhachHang = KhachHangInfo.RowID;
                    if (OnKhachHang_Changed != null)
                        OnKhachHang_Changed();
                }
            }
        }

        private void radDropDownList_KhachHang_SelectedValueChanged(object sender, EventArgs e)
        {
            
        }

        private void radMenuItem_TaoBanIn_Click(object sender, EventArgs e)
        {
            frm_RPPhieuNhanMau frm = new frm_RPPhieuNhanMau();
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        
    }
}

